package com.ss.android.auto.launch.tasks.collector.stage2

import com.bytedance.article.common.impression.EnvConfig
import com.bytedance.common.utility.concurrent.ThreadPlus
import com.bytedance.dataplatform.abTest.Experiments
import com.dcd.abtest.experiment.fps.MainTabSwitchOptExperiment
import com.dcd.abtest.experiment.launch.HomePageLaunchTaskOptExperiment
import com.ss.android.auto.automonitor_api.IAutoMonitorService
import com.ss.android.auto.feed.event.FeedEventHelper
import com.ss.android.auto.init.AppCoreData
import com.ss.android.auto.lancet.GetCacheDirLancet
import com.ss.android.auto.launch.WorkerMainThreadTask
import com.ss.android.auto.launch.helper.EventInitHelper
import com.ss.android.auto.launch.helper.ScEventHelper
import com.ss.android.auto.launch.initializer.FeedGoodInitializer
import com.ss.android.auto.launch.initializer.FishboneLoadingViewInitializer.init
import com.ss.android.auto.launch.tasks.collector.MainThreadTaskCollector
import com.ss.android.auto.launch.tasks.old.WorkerBackgroundTaskB
import com.ss.android.auto.optimize.serviceapi.IOptimizeService
import com.ss.android.auto.pangolin.PangolinInitializer
import com.ss.android.auto.servicemanagerwrapper.AutoServiceManager
import com.ss.android.auto.usedcar.IUsedCarService
import com.ss.android.auto.utils.NewUserUtils
import com.ss.android.common.app.AbsApplication
import com.ss.android.common.ui.view.LoadingFlashViewBase
import com.ss.android.event.EventFragment
import com.ss.android.globalcard.GlobalCardModule
import com.ss.android.globalcard.utils.NewFeedSwitchUtil

/**
 *  第二阶段主线程任务，按照顺序执行
 *  Warning！！这里面新增的都会增加启动耗时，请确认是否可以异步执行。
 *
 *  @author zhongxinyuan @ ByteDance Inc
 *  @since   2023/2/2
 */
object Stage2SyncTasks : MainThreadTaskCollector() {

    override fun collectSubTasks() {
        addSubTask("_WorkerMainThreadTask") {
            GetCacheDirLancet.enable = Experiments.getFpsMergeOpt(true)
        }
        addSubTask("tryPreRequestFeedData") {
            EventFragment.useNewFragmentVisibilityMgr = MainTabSwitchOptExperiment.isOpt()

            val optimizeService: IOptimizeService? =
                AutoServiceManager.getService2<IOptimizeService>(
                    IOptimizeService::class.java
                )
            if (!NewUserUtils.isNewUser() || !optimizeService!!.isNewUserLaunchOpt) {
                if (!optimizeService!!.isFeedRequestWaitDid) WorkerMainThreadTask.tryPreRequestFeedData()
            }
        }
        addSubTask("initBroadcastReceiver") {
            WorkerMainThreadTask.initBroadcastReceiverManager()
        }
        addSubTask("initMira") {
            WorkerMainThreadTask.initMira() // 插件初始化

        }
        addSubTask("initApm") {
            WorkerMainThreadTask.initApm()
        }
        addSubTask("EventInitHelper") {
            EventInitHelper.init(AbsApplication.getApplication()) // 埋点信息初始化

        }
        addSubTask("ScEventHelper") {
            ScEventHelper.init(AbsApplication.getApplication()) // Sc埋点处理，一定要放在EventInitHelper.init之后

        }
        addSubTask("initUIRelated") {
            WorkerMainThreadTask.initUIRelated() // UI 相关初始化

        }
        addSubTask("initDebugChecker") {
            WorkerMainThreadTask.initDebugChecker() // Debug 版本检测功能初始化
        }
        addSubTask("initBusinessModule") {

            GlobalCardModule.sErrorInfo += "-1"
            WorkerMainThreadTask.initBusinessModule() // 业务module初始化
            WorkerMainThreadTask.initBusinessFunction() // 业务功能点初始化
            GlobalCardModule.sErrorInfo += "6"
        }
        // 阻塞等待反作弊sdk初始化完成，实验组取消阻塞
        if (!HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("waitFutureTaskFinish") {
                WorkerMainThreadTask.waitFutureTaskFinish()
            }
        }
        addSubTask("FeedGoodInitializer") {
            FeedGoodInitializer.init()
        }
        addSubTask("setUseNewHomePage") {
            EnvConfig.isDebug = AppCoreData.getInstance().isDebug
            FeedEventHelper.isDebug = AppCoreData.getInstance().isDebug
            AutoServiceManager.getService2<IAutoMonitorService>(IAutoMonitorService::class.java)
                ?.setUseNewHomePage()
        }
        addSubTask("initBdPermission") {
            WorkerMainThreadTask.initBdPermission()
        }
        addSubTask("FishboneLoading") {
            init()
        }
        if (!HomePageLaunchTaskOptExperiment.open()) {
            //沟通可去除
            addSubTask("LoadingFlashViewBase") {
                if (AutoServiceManager.getService2<IUsedCarService>(IUsedCarService::class.java)
                    !!.isInLowEndProject()
                ) {
                    LoadingFlashViewBase.doLottieAni = false
                }
            }
            //延迟初始化
            addSubTask("initSmartRouter2") {
                WorkerMainThreadTask.initSmartRouter2()
            }
        }

//        addSubTask("initPangolin") {
//            PangolinInitializer.ensureInitialized()
//        }
    }

}